//在页面未加载完毕之前显示的loading Html自定义内容

//监听加载状态改变
document.onreadystatechange = completeLoading;

//加载状态为complete时移除loading效果
function completeLoading() {
    if (document.readyState == "complete") {
        var loadingMask = document.getElementById('loadingDiv');
        loadingMask.parentNode.removeChild(loadingMask);
    }
}
$(document).ready(function () {
    // 初始化内容
    // var loadingMask = document.getElementById('loadingDiv');
    // loadingMask.parentNode.removeChild(loadingMask);
    var peopleList = [];
    //获取班组简介
    var fd = new FormData();
    fd.append("json", JSON.stringify({
        "userId": userId,
        "data": {
            "bzId": bzId
        }
    }));
    // 人员简介字段渲染
    //值班表
    var DateTime = new Date();
    var year = DateTime.getFullYear();
    var month = DateTime.getMonth() + 1;
    var from = year + "-" + month + '-1';

    var to = "";
    if (month == 12) {
        to = (year + 1) + '-1-1'
    } else {
        to = year + "-" + (month + 1) + '-1';
    }
    $.ajax({
        url: baseUrl + 'api/Culture/GetMeetAbstract',
        // url:'http://10.36.1.77/bzapp/api/Culture/GetMeetAbstract',
        data: {
            json: JSON.stringify({
                "userId": userId,
                "data": {
                    "bzId": bzId
                }
            })
        },
        type: "POST",
        // contentType:false,
        // processData:false,
        success: function (data) {
            console.log(data);
            if (data.code == 0) {
                classinstru(data.data)
                $(".topLeft").html(data.data.BzName);
            }

        }
    })

    function classinstru(item) {
        var x = '<div class="m-item-j bg-color-font"><div class="circle bg-color-bg"></div><span>班组成员' + item.Member + '人</span><span>平均年龄' + (Number(item.AverageAage)).toFixed(0) + '岁</span></div>' +
            '<div class="m-item-j bg-color-font"><div class="circle bg-color-bg"></div><span>大专及以上学历' + item.Education + '人</span><span>中共党员' + item.PartyMember + '人</span></div>' +
            '<div class="m-item-j bg-color-font"><div class="circle bg-color-bg"></div><span>工程师' + item.Engineer + '人</span><span>助理工程师' + item.AssistantEngineer + '人</span><span>高级技师' + item.ExpertTechnician + '人</span><span>技师' + item.Technician + '人</span><span>高级工' + item.Expert + '人</span></div>'
        $("#m-job").html(x);
    }
    //获取文化墙信息
    $.ajax({
        // contentType:"application/json; charset=utf-8",
        url: baseUrl + 'api/Culture/GetWallInfo',
        data: fd,
        type: "POST",
        contentType: false,
        processData: false,
        success: function (data) {
            if (data.code == 0) {
                mainclass(data.data)
            }
        }
    })

    function mainclass(item) {
        $(".textareaW").html(item.summary);
        // $("#classhope").val('是滴是滴')
        $("#classhope").val(item.vision);
        if (item.slogan) {
            var xstring = item.slogan;
            if (xstring.length > 20) {
                xstring = xstring.substring(0, 20) + "...";
            }
            $(".titlep").html(xstring);
        }
        // console.log(document.getElementById("#classhop"))
        // document.getElementById("#classhop").innerHTML="11111"
        var arr = []; //班组荣誉
        var brr = []; //风采剪影
        item.pics.forEach(
            function (obj) {
                if (obj.filetype == 1) {
                    arr.push(obj);
                } else {
                    brr.push(obj);
                }
            }
        )
        console.log(arr, brr);
        var arrObj = "";
        var brrObj = "";

        if (arr.length > 3) {
            arr = arr.slice(0, 3)
        }
        if (brr.length > 3) {
            brr = brr.slice(0, 3)
        }
        for (var j = 0; j < arr.length; j++) {
            arrObj += '<div class="item-hover">' +
                '<img src="' + arr[j].filepath + '" alt="">' +
                '</div>'
        }
        $(".c-honer").html(arrObj);
        if (brr.length == 1) {
            brrObj += '<div class="c-c-left">' +
                '<img src="' + brr[0].filepath + '" alt="">' +
                '</div>' +
                '<a class="c-c-right" href="./detail/detail1.html?number=3&userId=' + userId + "&bzId=" + bzId + '">' +
                '<img src="./img/01/5.jpg" alt="">' +
                '</a>'
        }
        if (brr.length == 2) {
            brrObj += '<div class="c-c-left">' +
                '<img src="' + brr[0].filepath + '" alt="">' +
                '</div>' +
                '<div class="c-c-mid">' +
                '<img src="' + brr[1].filepath + '" alt="">' +
                // '<img class="newimg" src="'+brr[2].filepath+'" alt="">'+
                '</div>' +
                '<a class="c-c-right" href="./detail/detail1.html?number=3&userId=' + userId + "&bzId=" + bzId + '">' +
                '<img src="./img/01/5.jpg" alt="">' +
                '</a>'
        }
        if (brr.length > 2) {
            brrObj += '<div class="c-c-left">' +
                '<img src="' + brr[0].filepath + '" alt="">' +
                '</div>' +
                '<div class="c-c-mid">' +
                '<img src="' + brr[1].filepath + '" alt="">' +
                '<img class="newimg" src="' + brr[2].filepath + '" alt="">' +
                '</div>' +
                '<a class="c-c-right" href="./detail/detail1.html?number=3&userId=' + userId + "&bzId=" + bzId + '">' +
                '<img src="./img/01/5.jpg" alt="">' +
                '</a>'
        }
        $(".c-img-croper").html(brrObj);
    }
    // //保存、编辑文化墙信息
    // var SaveWallInfo = new FormData();
    // var saveParams ={
    //        userId:userId,
    //        data:{
    //         bzId:bzId,
    //         summary:"aaaaa",
    //         concept:"dssssssssssssssssssssss",
    //         slogan:"bbbbb",
    //         vision:"cccc"
    //        } 
    // }
    // SaveWallInfo.append(
    //     'json',JSON.stringify(saveParams)
    // )
    // $.ajax(
    //     {
    //         url:baseUrl+'api/Culture/SaveWallInfo',
    //         data:SaveWallInfo,
    //         type:"POST",
    //         contentType:false,
    //         processData:false,
    //         success:function(data){
    //             console.log(data);
    //         }
    //     }
    // )
    // //上传风采剪影、班组荣誉
    // var UpdateFile = new FormData();
    // var UpdateFileInfo ={
    //        userId:userId,
    //        data:{
    //         bzId:bzId,
    //         deletefileids:"",
    //         description:"",
    //         filetype:"0",
    //        } 
    // }
    // UpdateFile.append(
    //     'json',JSON.stringify(UpdateFileInfo)
    // )
    // $.ajax(
    //     {
    //         url:baseUrl+'api/Culture/UploadFile',
    //         data:UpdateFile,
    //         type:"POST",
    //         contentType:false,
    //         processData:false,
    //         success:function(data){
    //             console.log(data);
    //         }
    //     }
    // )
    // //获取班组成员
    var classpeople = new FormData();
    $.ajax({
        url: baseUrl + 'api/Culture/GetDeptMembers',
        type: "POST",
        data: {
            json: JSON.stringify({
                "userId": userId,
                "data": {
                    "bzId": bzId
                }
            })
        },
        success: function (data) {
            if (data.code == 0) {
                peopleList = data.data.slice(0, 6);
                getperson();
            }
            console.log(peopleList);
            // peopleList = 
        }
    })

    function getperson() {
        var x = "";
        for (var i = 0; i < peopleList.length; i++) {
            x += '<div class="pre-item">' +
                '<div class="pre-top">' +
                '<div class="pre-name">' + peopleList[i].Name + '</div>' +
                '<div class="p-j-job">' + peopleList[i].Quarters + '</div>' +
                '</div>' +
                '<div class="imgIcon">' +
                '<img src="' + (peopleList[i].Photo ? peopleList[i].Photo : './img/01/person.png') + '" alt="">' +
                '</div>' +
                '</div>'
        }
        $("#peopleQ").html(x);
    }
    // //月度工作任务评分
    $.ajax({
        url: baseUrl + 'api/Culture/GetMonthScore',
        data: {
            json: JSON.stringify({
                "userId": userId,
                "data": {
                    "year": year,
                    "month": month
                }
            })
        },
        type: "POST",
        // contentType:false,
        // processData:false,
        success: function (data) {
            // charts(data.data)
        }
    })

    function charts(item) {
        var chartName = [];
        var chartValue = [];

        item.forEach(
            function (list) {
                chartName.push(list.UserName);
                chartValue.push(list.Score);
            }
        )
        var obj = document.getElementById('charts')
        var option = {
            tooltip: {
                trigger: 'axis'
            },
            xAxis: [{
                type: 'category',
                data: chartName,
                axisLine: {
                    lineStyle: {
                        color: '#c85038'
                    }
                }
            }],
            // dataZoom: [
            //     {
            //         show: true,
            //         realtime: true,
            //         start: 0,
            //         end: 50
            //     },
            //     {
            //         type: 'inside',
            //         realtime: true,
            //         start: 0,
            //         end: 50
            //     }
            // ],
            yAxis: [{
                    type: 'value',
                    min: 0,
                    axisLabel: {
                        formatter: '{value}',
                        textStyle: {
                            color: '#c85038'
                        }
                    },
                    axisLine: {
                        lineStyle: {
                            color: '#c85038'
                        }
                    }
                },
                {
                    type: 'value',
                    min: 0,
                    axisLabel: {
                        formatter: '{value}',
                        textStyle: {
                            color: '#c85038'
                        }
                    },
                    axisLine: {
                        lineStyle: {
                            color: '#c85038'
                        }
                    }

                }
            ],
            series: [{
                name: '任务评分',
                type: 'bar',
                barWidth: '20',
                data: chartValue,
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            position: 'top',
                            color: '#c85038'
                        },
                        color: '#c85038',
                    },
                },
            }, ]
        };
        var Chart4 = echarts.init(obj)
        Chart4.setOption(option);
    }
    // //获取班组月考勤数据
    $.ajax({
        url: baseUrl + 'api/Attendance/GetDeptAttendance',
        data: {
            json: JSON.stringify({
                "userId": userId,
                "data": {
                    "year": year,
                    "month": month
                }
            })
        },
        type: "POST",
        // contentType:false,
        // processData:false,
        success: function (data) {
            studyDate(data.data)
        }
    })
    // target="_blank"
    function studyDate(list) {
        var arrlist = ""
        for (var i = 0; i < list.length; i++) {
            arrlist += '<div class="c-c-listmsg">' +
                '<div class="tv-title">' +
                // '<div class="tv-img">'+
                //     '<img src="'+(list[i].Photo?list[i].Photo:"./img/01/person.png")+'" alt="">'+
                // '</div>'+
                // '<div class="t-v-v">'+
                '<div class="t-name">' + list[i].UserName + '</div>' +
                // '<div class="t-job-t">'+(list[i].RoleName?list[i].RoleName:"")+'</div>'+
                // '</div>'+
                '</div>'
            if (list[i]['Data'][list[i]['Data'].length - 1].Category == "出勤") {
                arrlist += '<div class="to-works">' +
                    '<div class="page-number bg-color-font">' + list[i]['Data'][list[i]['Data'].length - 1].Times + '</div>' +
                    '<div class="page-num">' + list[i]['Data'][list[i]['Data'].length - 1].Category + '(次)</div>' +
                    '</div>'
            }
            // if(list[i]['Data'].length==9){
            //     arrlist += '<div class="to-works">'+
            //     '<div class="page-number bg-color-font">'+list[i]['Data'][list[i]['Data'].length-1].Times+'</div>'+
            //     '<div class="page-num">'+list[i]['Data'][list[i]['Data'].length-1].Category+'(次)</div>'+
            //     '</div>'
            // }else{
            //     arrlist += '<div class="to-works">'+
            //     '<div class="page-number bg-color-font">0</div>'+
            //     '<div class="page-num">出勤(次)</div>'+
            //     '</div>'
            // }
            for (var j = 0; j < list[i]['Data'].length - 1; j++) {
                if (list[i]['Data'][j].Category == "值班") {
                    arrlist += '<div class="to-works">' +
                        '<div class="page-number bg-color-font">' + list[i]['Data'][j].Times + '</div>' +
                        '<div class="page-num">' + list[i]['Data'][j].Category + '(次)</div>' +
                        '</div>'
                } else {
                    arrlist += '<div class="to-works">' +
                        '<div class="page-number bg-color-font">' + list[i]['Data'][j].Hours + '</div>' +
                        '<div class="page-num">' + list[i]['Data'][j].Category + '(h)</div>' +
                        '</div>'
                }
            }
            arrlist += '</div>'
        }
        $("#morework").html(arrlist);
    }

    $.ajax({
        url: baseUrl + 'api/Attendance/GetDutyData',
        data: {
            json: JSON.stringify({
                "userId": userId,
                "data": {
                    "from": year + "-" + month + '-1',
                    "to": to
                }
            })
        },
        type: "POST",
        // contentType:false,
        // processData:false,
        success: function (data) {
            var datapeople = data.data.pop();
            var xinqi = DateTime.getDay();
            var dateArr = [];
            data.data.forEach(
                function (item, index) {
                    dateArr.push({
                        Data: item.Data,
                        index: index + 1
                    })
                }
            )
            work(xinqi, dateArr)
        }
    })
    //创建值班表
    //创建值班表
    function work(index, list) {
        var worklist = "";
        worklist += '<tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr><tr>';
        var a = [];
        var c = [];
        var d = [];
        var DateTime = new Date();
        var days = DateTime.getDate();
        var setflag = 0;
        console.log(days, index)
        if (index == 0) {
            index = 7;
        }
        if (index > (days % 7)) {
            day = (index - (days % 7)) + 1;

        } else {
            day = 7 + (index - (days % 7)) + 1;
            setflag = days + day;
        }
        for (var k = 0; k < day; k++) {
            a.push({
                Data: ""
            })
        }
        c = a.concat(list);

        for (var w = 0; w < (42 - c.length) % 7; w++) {
            d.push({
                Data: ""
            })
        }
        c = c.concat(d);
        for (var q = 0; q < c.length / 7; q++) {
            worklist += '<tr>'
            for (var o = 0; o < 7; o++) {
                if ((q * 7 + o) == (days + day - 1)) {
                    worklist += '<td class="dayactive">' +
                        '<span>' + ((c[7 * q + o].index) ? (c[7 * q + o].index) : "") + '</span>' +
                        '<p>' + ((c[7 * q + o].Data) ? (c[7 * q + o].Data) : "") + '</p>' +
                        '</td>'
                } else {
                    worklist += '<td>' +
                        '<span>' + ((c[7 * q + o].index) ? (c[7 * q + o].index) : "") + '</span>' +
                        '<p>' + ((c[7 * q + o].Data) ? (c[7 * q + o].Data) : "") + '</p>' +
                        '</td>'
                }
            }
            worklist += '</tr>'
        }
        $("#newtable").html(worklist);
    }
    //获取学习园地
    $.ajax({
        url: baseUrl + 'api/Culture/GetLearningGardens',
        data: fd,
        type: "POST",
        contentType: false,
        processData: false,
        success: function (data) {
            console.log(data);
        }
    })
});


//月度工作任务

var DateTimeNew = new Date();
var yearnew = DateTimeNew.getFullYear();
var monthnew = DateTimeNew.getMonth() + 1;
if (monthnew == 1) {
    yearnew--;
    monthnew = 12;
} else {
    // monthnew--;
}

if (monthnew < 10) {
    monthnew = "0" + monthnew;
}

var fromnew = yearnew + "-" + monthnew + "-01"

var fq = new FormData();
fq.append("json", JSON.stringify({
    "userId": userId,
    "data": {
        // "bzId":bzId,
        "time": fromnew,
        "departmentid": bzId
    }
}));
$.ajax({
    url: baseUrl + 'api/Performance/PerformanceSelect',
    data: {
        json: JSON.stringify({
            "userId": userId,
            "data": {
                "time": fromnew,
                "departmentid": bzId
            }
        })
    },
    type: "POST",
    // contentType:false,
    // processData:false,
    success: function (data) {
        console.log(data);
        if (data.code == 0) {
            mainTitle(data.data)
        }

        // studyarea(data.data);
    }
})

function mainTitle(data) {
    console.log(data);
    var sorceList = [];
    var titleThing;
    var monthD = DateTimeNew.getMonth();
    if(monthD == 0){
        monthD = 12
    }
    if (!data) {
        var newDateMsg = "<span>" + monthD + "月份绩效考核</span>" +
            "<span>班组绩效总额0元</span>";
        $("#warning").css("display", "block");
        $("#newDate").html(newDateMsg);
        $(".data-table").css("display", "none");
        return false;
    }
    var upstatus = data.up;
    if (!upstatus) {
        var newDateMsg = "<span>" + monthD + "月份绩效考核</span>" +
            "<span>班组绩效总额0元</span>";
        $("#warning").css("display", "block");
        $("#newDate").html(newDateMsg);
        $(".data-table").css("display", "none");
        return false;
    }
    sorceList = data.score;
    titleThing = data.title;
    var nameList = (titleThing.name).split(",");

    var strTable = "<tbody></tr>";
    strTable += "<th></th>"

    var tabletop;
    for (var i = 0; i < nameList.length; i++) {
        tabletop += '<th>' + nameList[i] + "</th>";
    }
    $("#table-top").html(tabletop)
    strTable += "</tr>";
    var scoreValue = 0;

    var tablecontent;

    var tableend;
    for (var j = 0; j < sorceList.length; j++) {
        tablecontent += Number((sorceList[j]['score'].split(","))[0]);

        tablecontent += "<tr>"
        tableend += '<tr><td>' + sorceList[j]['username'] + '</td></tr>'
        var scorechange = sorceList[j]['score'].split(",");
        var sortchange = sorceList[j]['sort'].split(",");
        for (var k = 0; k < nameList.length; k++) {
            tablecontent += '<td>' + scorechange[Number(sortchange[k])] + "</td>";

        }
        scoreValue += Number(scorechange[0]);
        tablecontent += "</tr>"
    }
    $("#tob-content").html(tablecontent);
    $("#table-end").html(tableend);
    var newDateMsg = "<span>" + monthD + "月份绩效考核</span>" +
        "<span>班组绩效总额" +
        scoreValue +
        "元</span>";
    newtable()




    strTable += "</tbody>";
    if (sorceList.length == 0) {
        // $("#warning").css("display","block");
    } else {
        $("#new-meassage").html(strTable);
        $("#newDate").html(newDateMsg)
    }

}

var month = (new Date()).getMonth() + 1;
setTimeout(
    function () {
        $("#kaoqin").html('<span>' + month + '月考勤表</span>');
        $("#zhiban").html('<span>' + month + '月值班表</span>');
    }, 2000
)

//班务接口
$.ajax({
    url: baseUrl1 + 'api/MenuConfig/ModuleShow',
    data: {
        json: JSON.stringify({
            id: regDeptId,
            themetype: 0
        })
    },
    type: "POST",
    // contentType:false,
    // processData:false,
    success: function (data) {
        var newdata = data.Data;
        if (newdata.kqb == 0) {
            $("#select-1").remove();
            $("#select-4").remove();
        }
        if (newdata.zbb == 0) {
            $("#select-2").remove();
            $("#select-5").remove();
        }
        if (newdata.jxkhb == 0) {
            $("#select-3").remove();
            $("#select-6").remove();
        }
        $('.c-list').eq(0).addClass('bg-color-bg');
        $('.c-list-node').eq(0).addClass('active');
    }
})